package com.sh.data.engine.application.service.normalization;

import cn.hutool.core.convert.Convert;
import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.normalization.standard.*;
import com.sh.data.engine.application.model.mapper.normalization.DictionaryResponsePageMapper;
import com.sh.data.engine.application.model.request.normalization.*;
import com.sh.data.engine.common.exception.InvalidParamException;
import com.sh.data.engine.common.util.ServletUtils;
import com.sh.data.engine.common.util.excel.EasyExcelUtil;
import com.sh.data.engine.domain.base.model.PageResult;
import com.sh.data.engine.domain.base.model.UserContext;
import com.sh.data.engine.domain.normalization.research.themedesign.model.domain.ThemeDomain;
import com.sh.data.engine.domain.normalization.standard.model.domain.DictionaryDetailDomain;
import com.sh.data.engine.domain.normalization.standard.model.domain.DictionaryDomain;
import com.sh.data.engine.domain.normalization.standard.model.enums.DataTypeEnum;
import com.sh.data.engine.domain.normalization.standard.model.enums.UnitMeasureEnum;
import com.sh.data.engine.domain.normalization.standard.model.param.DictionaryOptParam;
import com.sh.data.engine.domain.normalization.standard.model.param.DictionaryParam;
import com.sh.data.engine.domain.normalization.standard.model.param.DictionaryQueryParam;
import com.sh.data.engine.domain.normalization.standard.service.IDictionaryService;
import com.sh.data.engine.domain.util.ConvertUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * @author: mengzheng.mhc
 * @date: 2024/7/3 13:49
 */
@Slf4j
@Service
public class DictionaryApplicationService {

    @Autowired
    private IDictionaryService dictionaryService;

    @Autowired
    private DictionaryResponsePageMapper dictionaryResponsePageMapper;

    public DictionaryConsResponse consList() {
        DictionaryConsResponse response = new DictionaryConsResponse();
        response.setDataTypeList(DataTypeEnum.getKeyValueList());
        response.setUnitMeasureList(UnitMeasureEnum.getKeyValueList());
        return response;
    }

    public boolean addDictionary(DictionaryRequest request) {
        if (request.getDictType() == 0) {
            Assert.notNull(request.getDictDataType(), "请选择数据类型");
        }
        DictionaryParam dictionaryParam = ConvertUtil.copyProperties(request, DictionaryParam.class);
        return dictionaryService.addDictionary(dictionaryParam);
    }

    public List<DictionaryGroupResponse> getThemesList() {
        Long projectId = ServletUtils.getProjectId();
        List<ThemeDomain> themesForStand = dictionaryService.getThemesForStand(projectId);
        return ConvertUtil.copyProperties(themesForStand, DictionaryGroupResponse.class);
    }

    public boolean editDictionary(DictionaryRequest request) {
        Assert.notNull(request.getId(), "id不能为空");
        if (request.getDictType() == 0) {
            Assert.notNull(request.getDictDataType(), "请选择数据类型");
        }
        DictionaryParam dictionaryParam = ConvertUtil.copyProperties(request, DictionaryParam.class);
        return dictionaryService.editDictionary(dictionaryParam);
    }

    public PagerResponse<DictionaryResponse> listDictionary(DictionaryQueryRequest request) {
        DictionaryQueryParam dictionaryQueryParam =
            ConvertUtil.copyProperties(request, DictionaryQueryParam.class);
        Long projectId = ServletUtils.getProjectId();
        dictionaryQueryParam.setProjectId(projectId);
        PageResult<DictionaryResponse> pageResult = ConvertUtil.convertPageInfo(
            dictionaryService.listDictionary(dictionaryQueryParam), DictionaryResponse.class);
        return dictionaryResponsePageMapper.toPageResponse(pageResult);
    }

    public boolean deleteDictionary(DictionaryOptRequest request) {
        String userId = UserContext.getUserId();
        dictionaryService.deleteDictionary(
            userId, ConvertUtil.copyProperties(request, DictionaryOptParam.class));
        return true;
    }

    public boolean releaseDictionary(DictionaryOptRequest request) {
        if (request.getReleaseStatus() == null
            || request.getReleaseStatus() < 0
            || request.getReleaseStatus() > 1) {
            throw new InvalidParamException("发布状态参数不合法");
        }
        String userId = UserContext.getUserId();
        dictionaryService.releaseDictionary(
            userId, ConvertUtil.copyProperties(request, DictionaryOptParam.class));
        return true;
    }

    public DictionaryDetailResponse detailDictionary(DictionaryOptRequest request) {
        DictionaryDetailDomain dictionaryDetailDomain =
            dictionaryService.detailDictionary(request.getId());
        if (Objects.isNull(dictionaryDetailDomain)) {
            return new DictionaryDetailResponse();
        }
        return ConvertUtil.copyProperties(dictionaryDetailDomain, DictionaryDetailResponse.class);
    }

    public boolean importExcel(MultipartFile file) {
        int headRowNumber = 3;
        List<List<String>> dataList = EasyExcelUtil.singleSheetImport(file, headRowNumber);
        dictionaryService.importDictionary(dataList, headRowNumber);
        return true;
    }

    public boolean batchOpt(DictionaryBatchOptRequest request) {
        if (CollectionUtils.isEmpty(request.getIds())) {
            throw new InvalidParamException("请选择标准");
        }
        if (Objects.isNull(request.getOptType())
            || request.getOptType() < 0
            || request.getOptType() > 2) {
            throw new InvalidParamException("操作类型值不正确");
        }
        String userId = UserContext.getUserId();
        dictionaryService.batchOpt(userId, request.getIds(), request.getOptType());
        return true;
    }

    public void exportList(DictionaryBatchOptRequest request, HttpServletResponse response) {
        if (CollectionUtils.isEmpty(request.getIds())) {
            throw new InvalidParamException("请选择需要导出的标准");
        }
        dictionaryService.exportList(request.getIds(), response);
    }

    public GetDicInfoByFieldNamesResponse getDicInfoByFieldNames(GetDicInfoByFieldNamesRequest request) {
        List<DictionaryDomain> dictionaryByFiledName =
            dictionaryService.getDictionaryByFiledName(request.getFieldNames(), request.getProjectId());
        return Convert.convert(GetDicInfoByFieldNamesResponse.class, dictionaryByFiledName);
    }
}
